<?php
/**
+--------------------------------------------------------------
 * User: maqun
+--------------------------------------------------------------
 * Date: 2015/12/9
+--------------------------------------------------------------
 * Time: 10:35
+--------------------------------------------------------------
 * 排名列表
+--------------------------------------------------------------
 */
class RankAction extends BaseAction{

    protected  $page_size = 20;
    protected  $tag       = array();

    public function _initialize(){
        self::$action = 4;
        parent::_initialize();
        for($i=2011;$i<=date("Y",time());$i++){
            $this->tag[] = $i;
        }
    }
    public function index(){
//        $grade = D('Users')->getGradeInfo();//获取年级
        $start_date = !empty($_GET['start_date']) ? date('Y-m-d H:i:s',strtotime($_GET['start_date'])):'';
        $end_date   = !empty($_GET['end_date']) ? date('Y-m-d H:i:s',strtotime($_GET['end_date'])):'';
        $user_id    = !empty($_GET['user_id'])  ? $this->filterUserid($_GET['user_id']) : '';
        $sub_grade  = !empty($_GET['grade']) && in_array(intval($_GET['grade']),$grade)? intval($_GET['grade']):'';

        $time_tag   = !empty($_GET['time_tag']) ? $_GET['time_tag'] : 0;

        if(!empty($time_tag) && empty($start_date) && empty($end_date)){
            $end_date = date('Y-m-d',time())." 23:59:59";
            $day = date('d',time());
            $month = date('m',time());
            $year = date('Y',time());
            $week = date('N',time());
            switch(intval($time_tag)){
                case 1:$start_date = date('Y-m-d',time())." 00:00:00";break;//按天
                case 2:$start_date = date('Y-m-d H:i:s',strtotime($year.'-'.$month.'-'.($day-$week%8+1)));break;//周
                case 3:$start_date = date('Y-m-d H:i:s',strtotime($year.'-'.$month.'-01'));break;//月
                case 4:$start_date = date('Y-m-d H:i:s',strtotime($year.'-01-01'));break;//年
                default:break;
            }
        }else{
            unset($time_tag);
        }
        if(!empty($user_id)){
            $this->redirect('User/index/',array('id'=>$user_id));
        }
        vendor('Page');
        if(empty($start_date)&&empty($end_date)&&empty($sub_grade)){

            $total_sql = "select count(DISTINCT (`user_id`)) as total from `users` where `submit`>0 ";
            $count = D('Users')->query($total_sql);
            $count = !empty($count[0]['total']) ? intval($count[0]['total']):0;
            $page  = new Page($count,$this->page_size);
            $sql   = "select `user_id`,`nick`,`solved`,`submit` from users where `submit`>0 order by  `solved` DESC,`submit` DESC,`reg_time` limit ".$page->firstRow.",".$page->listRows;
            $list  = D('Users')->query($sql);
        }else{
            $where  = " where 1  ";

            if(!empty($start_date)&&!empty($end_date)){
                $where .= " and unix_timestamp(in_date) between ".strtotime($start_date)." and ".strtotime($end_date)." ";
            }
            if(!empty($sub_grade)){
                $where .= " and  user_id like '".$sub_grade."%' ";
            }
            $table1 = " (select count(distinct(problem_id)) as solved,user_id from solution {$where} and result=4 group by user_id) ";
            $table2 = " (select count(problem_id) as submit,user_id from solution {$where} group by user_id) ";

            $total_sql = "select count(a.user_id) as total from users as a right join {$table1} as b on a.user_id=b.user_id left join {$table2} as c on a.user_id=c.user_id where a.submit>0";
            $res = D('Users')->query($total_sql);
            $count = !empty($res[0]['total']) ? intval($res[0]['total']):0;
            $page  = new Page($count,$this->page_size);


            $sql   = "select  a.user_id,a.nick,a.reg_time,b.solved,c.submit from users as a right join {$table1} as b on a.user_id=b.user_id left join {$table2} as c on a.user_id=c.user_id where a.submit >0 order by b.solved DESC,c.submit DESC,a.reg_time limit ".$page->firstRow.",".$page->listRows;
            $list  = D('Users')->query($sql);
            echo $sql;dump($list);exit;
        }
        $this->assign('grade',$this->tag);
        $this->assign('start_date',$start_date);
        $this->assign('end_date',$end_date);
        $this->assign('user_id',$user_id);
        $this->assign('sub_grade',$sub_grade);
        $this->assign('list',$list);
        $this->assign('page',$page->show());
        $this->assign('time_tag',$time_tag);
        $this->assign('p',intval($_GET['p']));
        $this->display('index');

    }
}